/**
 * 
 */
package sqs.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.naming.InitialContext;

import sqs.ejb.sessionbean.QuestionnaireMgr;
import sqs.ejb.sessionbean.UserMgr;
import sqs.exception.MandatoryQnNotAnsException;
import sqs.exception.QuestionInactiveException;
import sqs.exception.QuestionnaireEnteredBeforeException;
import sqs.model.Answer;
import sqs.model.MCQQuestion;
import sqs.model.Option;
import sqs.model.Question;
import sqs.model.Questionnaire;
import sqs.model.SubmittedQuestionnaire;
import sqs.model.User;

/**
 * @author kjleng
 *
 */
public class TestSubmitQuestionnaire {
	public static void main(String[] args) {
		try {
			TestSubmitQuestionnaire client = new TestSubmitQuestionnaire();
			client.submitQuestionnaire(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void submitQuestionnaire(int qId) throws Exception {
		InitialContext ctx = new InitialContext();
		QuestionnaireMgr mgr = (QuestionnaireMgr)ctx.lookup("ejb/QuestionnaireJNDI");
		UserMgr userMgr = (UserMgr)ctx.lookup("ejb/UserJNDI");
		User respondent = userMgr.getUserByUserName("kjleng2");
		
		Questionnaire questionnaire = mgr.getQuestionnaireByQuestionnaireId(qId);
		if(questionnaire.enterQuestionnaireBefore(respondent)){
			throw new QuestionnaireEnteredBeforeException();
		}
		if(!questionnaire.isQuestionnaireActive()){
			throw new QuestionInactiveException();
		}
		
		SubmittedQuestionnaire sq = new SubmittedQuestionnaire();
		sq.setQuestionnaire(questionnaire);
		sq.setRespondent(respondent);
		sq.setSubmissionDate(new Date());
		
		answerQuestionnaire(questionnaire);
		try{
			questionnaire.areAllMandatoryQuestionsAnswered();
			mgr.submitQuestionnaire(sq);
		}catch(MandatoryQnNotAnsException e){
			Question unansweredQuestion = e.getUnAnsweredQuestion();
			System.out.println("Questionn " +unansweredQuestion.getQuestionId()+ " is not answered!");
			throw e;
		}
		System.out.println("Questionnaire submitted!!!!");
	}
	
	public void answerQuestionnaire(Questionnaire questionnaire) throws Exception {
		List<Question> questions = questionnaire.getQuestions();
		Iterator<Question> iter = questions.iterator();
		
		//display questions and options to user
		displayQuestions(iter);
		
		/***
		 * --- IMPORTANT --
		 * Implementation of the frontend need not have the hard-coding of questions.
		 * The command object of Spring should be able to dynamic binding
		 * The frontend should not worry about what is the question as it should all questions the same 
		 * as it is programmed to the abstract class, Question
		 * 
		 */
		
		// 1st question: MultipleAnsMCQQuestion
		Question q1 = questions.get(0);
		List<Option> options1 = q1.getOptions();
		
		//select option 1 and 2 for as answer Q1
		Option o1 = options1.get(0);
		Option o2 = options1.get(1);
		
		System.out.println("!!!! selecting ans for Q1");
		Answer ans = q1.createAnswer();
		ans.addOption(o1);
		ans.addOption(o2);
		q1.addAnswer(ans);
			
		// 2nd question: RatingQuestion
		Question q2 = questions.get(1);
		List<Option> options2 = q2.getOptions();
		//select option 1 as answer Q2
		Option ro1 = options2.get(0);
		System.out.println("!!!! selecting ans for Q2");
		Answer ans2 = q2.createAnswer();
		ans2.addOption(ro1);
		q2.addAnswer(ans2);
		
		// 3rd question: RatingQuestion
		Question q3 = questions.get(2);
		List<Option> options3 = q3.getOptions();
		//select option 1 as answer Q3
		Option o3 = options3.get(0);
		System.out.println("!!!! selecting ans for Q3");
		Answer ans3 = q3.createAnswer();
		ans3.addOption(o3);
		q3.addAnswer(ans3);
		
		// 4th question: SingleAnsQuestion
		Question q4 = questions.get(3);
		List<Option> options4 = q4.getOptions();
		//select option 1 as answer Q4
		Option o4 = options4.get(0);
		System.out.println("!!!! selecting ans for Q4");
		Answer ans4 = q4.createAnswer();
		ans4.addOption(o4);
		q4.addAnswer(ans4);
		
		// 5th question: NumericQuestion
		Question q5 = questions.get(4);
		//enter answer for Q5
		System.out.println("!!!! enter ans for Q5");
		Answer ans5 = q5.createAnswer();
		ans5.setNumericValue(32);
		q5.addAnswer(ans5);
	}
	
	public void displayQuestions(Iterator<Question> iter){
		while(iter.hasNext()){
			Question q = iter.next();
			List<Option> options = q.getOptions();
			
			//MCQ or Rating Question
			if(q.getLayout()!=null){
				if(q.getLayout().equals(Question.HORIZONTAL_LAYOUT)){
					//display options horizontally
					System.out.println("display options horizontally");
					Iterator<Option> i = options.iterator();
					System.out.println("Options available for Question!!!!!!!!!!");
					while(i.hasNext()){
						Option op = i.next();
						System.out.println("op = " +op.getOptionText());
					}
				}
				if(q.getLayout().equals(Question.HORIZONTAL_LAYOUT)){
					//display options vertically
					System.out.println("display options vertically");
					Iterator<Option> i = options.iterator();
					System.out.println("Options available for Question!!!!!!!!!!");
					while(i.hasNext()){
						Option op = i.next();
						System.out.println("op = " +op.getOptionText());
					}
				}
			}
			// Other questions
			else{
				Iterator<Option> i = options.iterator();
				System.out.println("Options available for Question!!!!!!!!!!");
				while(i.hasNext()){
					Option op = i.next();
					System.out.println("op = " +op.getOptionText());
				}
			}
		}
	}
	
	
}
